/**
 * DataTables
 */
var datatable = null, idList = [];

$(function () {
    datatable = $('.table-sort').DataTable({
        order: [
            [8, 'asc']
        ],
        ajax: {
            url: contextPath + "/menu/getPageList",
            type: 'post',
            data: function (d) {
                d.search = $('#search').val();
            }
        },
        columns: [{
            data: "id"
        }, {
            data: "name"
        }, {
            data: "urlkey"
        }, {
            data: "parentName",
            orderable: false,
            defaultContent: ""
        }, {
            data: "type",
            defaultContent: ""
        }, {
            data: "url",
            defaultContent: ""
        }, {
            data: "createTime",
            defaultContent: ""
        }, {
            data: "createName",
            orderable: false,
            defaultContent: ""
        }, {
            data: "sort",
            defaultContent: ""
        }, {
            data: "status",
            defaultContent: ""
        }, {
            data: null
        }],
        columnDefs: [{
            targets: [0],
            orderable: false,
            render: function (data, type, row, meta) {
                return '<input id="input-' + data + '" type="checkbox" name="single"><label for="input-' + data + '"></label>';
            }
        }, {
            targets: [4],
            render: function (data, type, row, meta) {
                // if (data==1){//1,根目录,2,a标签,3,a标签_blank,4,外部url
                switch (data) {
                    case 1 :
                       return '根目录';
                    case 2 :
                        return 'a标签';
                    case 3 :
                        return 'a标签_blank';
                    case 4 :
                        return '外部url';
                }
            }
        }, {
            targets: [6],
            render: function (data, type, row, meta) {
                return data ? new Date(data).pattern("yyyy-MM-dd hh:mm:ss") : '';
            }
        }, {
            targets: [10],
            orderable: false,
            responsivePriority: 1,
            render: function (data, type, row, meta) {
                var btns = new Array('edit', 'del');
                return getDTOperateBtn(btns, row.id, 'system-menu-add.jsp', null, '/menu/del/', reloadTable);
            }
        }]
    });
});

/**
 * 刷新DT
 */
function reloadTable() {
    datatable.ajax.reload(null, false);
}

/**
 * 获取datatables选中行的ID
 */
function getDTSelect() {
    var lines = datatable.rows('.selected').data();
    for (var i = 0; i < lines.length; i++) {
        idList.push(lines[i].id);
    }
    return idList;
}